<?php
namespace Home\Controller;

class IndexController extends CommonController {

    public function index() {
        $this->info['myphoto'] = '';
        if ($this->openid)
            $this->info['myphoto'] = M('Photos')->where(array('openid' => $this->openid, 'status' => '1'))->find();

        $this->assign($this->info);
        $this->display();
    }

    //上传照片页
    public function upload_view() {
        //是否已关注且写cookie
        if ($this->is_care == 0)
            $this->redirect('Pre/code');

        //是否已验证过白名单
        if (!$member = M('Member')->where(array('openid' => $this->openid))->find())
            $this->redirect('validate_view');

        $this->assign($this->info);
        $this->display();
    }

    //验证会员身份页面
    public function validate_view() {
        //是否已关注且写cookie
        if ($this->is_care == 0)
            $this->redirect('Pre/code');

        $this->assign($this->info);
        $this->display();
    }

    //确定上传
    public function confirm_view() {
        if (IS_POST) {
            M('Photos')->where(array('id' => I('photos_id')))->save(array('status' => '1'));
            $this->success("上传成功", U('photo', array('id' => I('photos_id'))));
        } else {
            //是否已关注且写cookie
            if ($this->is_care == 0)
                $this->redirect('Pre/code');

            $this->info['item'] = M('Photos')->find(I('photos_id'));
            $this->assign($this->info);
            $this->display();
        }
    }

    /**
     * 验证会员身份操作
     * localhost/redcard/index.php?m=Home&c=Index&a=validate_member&ajax=1&card_num=0783&member_num=0200016024&phone=18862175792
     */
    public function validate_member() {
        $data   = I('post.');
        $Member = M('Member');

        if (!$data['card_num'] || !$data['member_num'] || !$data['phone'])
            $this->error("请填入必要数据");

        $row = $Member->where(array('card_num' => $data['card_num'], 'member_num' => $data['member_num']))->find();
        if (!$row) {
            $this->error("请核对信息后重新填写");
        } elseif ($row['openid'] != '') {
            $this->error("该信息已被其他用户使用");
        }

        //更新手机号和openid到用户表
        $Member->where(array('id' => $row['id']))->save(array('phone' => $data['phone'], 'openid' => $this->openid));

        //更新手机号和卡号等到微信用户表
        M('WxMember')->where(array('fromuser' => $this->openid))->save(array(
            'card_num' => $data['card_num'],
            'member_num' => $data['member_num'],
            'phone' => $data['phone']
        ));

        $this->success("验证成功");
    }

    /**
     * 发布旅行照
     * localhost/redcard/index.php?m=Home&c=Index&a=photos_add&ajax=1&pic=
     */
    public function photos_add($pic = '') {
        if (empty($pic))
            $this->error('请上传照片');
        if ($photo = M('Photos')->where(array('openid' => $this->openid, 'status' => '1'))->find())
            $this->error('您已经晒过旅行照');
        //添加照片记录
        $res = M('Photos')->add(array('openid' => $this->openid, 'pic' => $pic, 'status' => '2'));
        $this->success($res);
    }

    /**
     * 照片列表页
     * localhost/redcard/index.php?m=Home&c=Index&a=lists
     */
    public function lists() {
        $p = I('p') ? intval(I('p')): 1;//页数
        $where['status'] = '1';
        if (I('id')) {
            $where['id'] = I('id');
            $row = M('Photos')->find(I('id'));
            if ($row['status'] == '3' && $this->openid == $row['openid']) {
                $this->info['errMsg'] = '啊哦，您上传的照片有些不妥，请再选择一张上传吧！';
            } else {
                $this->info['errMsg'] = '对不起你查找的照片不存在';
            }
        }

        $list = M('Photos')->where($where)->order("num desc, id asc")->page($p, 6)->select();
        foreach ($list as $key => $value) {
            $list[$key]['index'] = $this->getRankNum($value);
        }

        $this->info['list'] = $list;
        $this->assign($this->info);
        $this->display(IS_AJAX ? 'lists_loop' : 'lists');
    }

    /**
     * 某张照片展示页
     * localhost/redcard/index.php?m=Home&c=Index&a=photo&id=100001
     */
    public function photo($id) {
        $item = M('Photos')->find($id);
        $this->info['item']    = $item;
        $this->info['myPhoto'] = $this->openid == $item['openid'] ? 1 : 0;//属于我的照片

        //这张照片的排名
        $this->info['rankNum'] = $this->getRankNum($item);

        //分享文案
        if ($this->info['myPhoto'] == 1) {
            $this->info['shareTitle'] = '我的照片编号是'.$id.'，求点赞赢4999元旅行基金！';
        } else {
            $this->info['shareTitle'] = '厉害了！求赞照片编号'.$id.'，赢4999元旅行基金！';
        }
        
        $this->info['shareUrl']  = U('Home/Index/photo', array('id' => $id), false, true);//分享的地址是当前页面
        $this->info['shareImgUrl']  = C('IMG_PREFIX').$item['pic'];

        $this->assign($this->info);
        $this->display();
    }

    /**
     * 点赞操作
     * localhost/redcard/index.php?m=Home&c=Index&a=praise&ajax=1&photos_id=100001
     */
    public function praise($photos_id = 0) {
        $nowDate = date('Y-m-d');
        $row = M('Praise')->where(array('openid' => $this->openid, 'create_date' => $nowDate))->find();
        if ($row) $this->error("每天只可点赞一次,请明天再来吧!");

        if (M('Photos')->where(array('id' => $photos_id))->getField('openid') == $this->openid)
            $this->error("不能为自己点赞！");

        M('Praise')->add(array('openid' => $this->openid, 'photos_id' => $photos_id, 'create_date' => $nowDate));
        M('Photos')->where(array('id' => $photos_id))->setInc('num');//+1
        $this->success("点赞成功!");
    }

    /**
     * 获取广告数据
     * localhost/redcard/index.php?m=Home&c=Index&a=getAd&ajax=1&type=
     */
    public function getAd($type = '1') {
        $Ad = M('Ad');
        $Ad->where(array('type' => $type))->order('id desc');
        if ($type == '2') $Ad->limit(5);
        $list = $Ad->select();
        $this->ajaxReturn($list ? $list : array());
    }

    //计算照片排名
    public function getRankNum($photoRow) {
        $greaterNum = M('Photos')->where(array('num' => array('gt', $photoRow['num']), 'status' => '1'))->count();//大于该照片的数
        $eqNum = M('Photos')->where(array('num' => $photoRow['num'], 'id' => array('lt', $photoRow['id']), 'status' => '1'))->count();//等于但上传比该照片早的数
        $rankNum = $greaterNum + $eqNum + 1;
        return $rankNum;
    }

}